<?php

namespace Modules\Goods\Http\Controllers;

use function Couchbase\defaultDecoder;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Goods\Entities\Attr;
use Modules\Goods\Entities\Goods;
use Modules\Goods\Entities\Shop;

class ShopController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index($gid)
    {
//        dd($gid);
        //获取当前商品的所有货品数据
        $goods = Goods::find($gid);
        //获取所有的货品数据
        $shops = Shop::where('goods_id',$gid)->get();
        return view('goods::shops.index',compact('shops','goods','gid'));
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create($gid)
    {
        //获取当前商品的所有货品数据数据
        $goods = Goods::find($gid);

        //调用模型的方法来找到当前货品可用的属性
        $topAttrs = Shop::gettopAttrs($gid);

        return view('goods::shops.create',compact('goods','topAttrs','gid'));
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(Request $request,$gid)
    {
        //获取提交的所有货品数据
        $post = $request->all();
//        dump($post);
        //把获取到的数据分别一一对应的存入货品表中
        $post['goods_id'] = $gid;
        $post['attrs'] = implode(',',$post['attrs']);
//        dd($post['attrs']);
        Shop::create($post);
        //设置闪存，并跳转
        session()->flash('success','货品添加成功');
        return redirect()->route('shop',$gid);
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('goods::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit($gid,$id)
    {
//        dd($id);
        //获取当前商品的所有货品数据
        $shop = Shop::find($id)->toArray();
//        dd($shop);
        //把货品数据中的attrs字符串切割成数组
        $aa = explode(',',$shop['attrs']);
//        dd($aa);
        //调用模型的方法来找到当前货品可用的属性
        $topAttrs = Shop::gettopAttrs($gid);
//        dd($topAttrs->toArray());

        return view('goods::shops.edit',compact('gid','goods','shop','topAttrs','aa'));
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(Request $request,$id,$gid)
    {
//        dd($id);
        //获取编辑后提交的货品数据
        $post = $request->all();
//        dd($post);
        //获取对应货品的所有数据
        $shops = Shop::find($id);
//        dd($shops);
        //然后把更改后的数据一一替换掉原来的旧数据
        $shops->sname = $request->input('sname');
        $shops->price = $request->input('price');
        $shops['attrs'] = implode(',',$post['attrs']);
        $shops->kucun = $request->input('kucun');

        //保存一下
        $shops->save();
        //设置闪存，并跳转
        session()->flash('success','货品编辑成功');
        return redirect()->route('shop',$gid);
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy($id)
    {
//        dd($id);
        Shop::destroy($id);
        session()->flash('success','货品删除成功');
        return back();
    }
}
